define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon_code_goods/index' + location.search,
                    add_url: 'coupon_code_goods/add',
                    edit_url: 'coupon_code_goods/edit',
                    del_url: 'coupon_code_goods/del',
                    multi_url: 'coupon_code_goods/multi',
                    table: 'coupon_code_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        /*{field: 'icon', title: __('Icon'), events: Table.api.events.image, formatter: Table.api.formatter.image},*/
                        /*{field: 'citiesname', title: __('权益地市')},
                        {field: 'brand.username', title: __('Brand_id')},*/
                        {field: 'price', title: __('面值')},
                        {field: 'retail_price', title: __('非会员价')},
                        {field: 'price1', title: __('会员价')},
                        {field: 'cost_price', title: __('采购价')},
                        // {field: 'types_text', title: __('Coupon_id')},
                        {field: 'is_enable', title: __('Is_enable'), formatter: Table.api.formatter.toggle},
                        {field: 'tuijian', title: __('首页推荐'), formatter: Table.api.formatter.toggle},
                        // {field: 'sales', title: __('Sales')},
                        {field: 'sort', title: __('Sort')},
                        {field: 'add_time', title: __('Add_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-coupon_code").data("params", function (obj) {
                return {"custom[brand_id]":$("#c-brand_id").val(),"custom[Is_enable]":"1"};
               // return {custom: {brand_id: $("#c-brand_id").val()}},{custom: {type: $("#flag").val()}};
            });
            $("#c-coupon_code_class_id").change(function () {
                var a = $("#c-coupon_code_class_id") .val();
                var s
                $.post({
                    url: 'coupon_code_class/getOne',
                    type: 'POST',
                    dataType: 'json',
                    data: {id: a},
                    cache: false,
                    success: function (ret) {
                        if (ret == 2) {
                            $("#brand") . hide();
                        } else if (ret == 1) {
                            $("#brand") . show();
                        } else {
                            $("#brand") . hide();
                        }
                    }, error: function () {
                        Toastr.error(__('Network error'));
                    }
                });
            })
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-coupon_code").data("params", function (obj) {
                return {custom: {brand_id: $("#c-brand_id").val()},"custom[Is_enable]":"1"};
            });
            $("#c-coupon_code").data("params", function (obj) {
                return {"custom[brand_id]":$("#c-brand_id").val(),"custom[Is_enable]":"1"};
                // return {custom: {brand_id: $("#c-brand_id").val()}},{custom: {type: $("#flag").val()}};
            });
            $("#c-coupon_code_class_id").change(function () {
                var a = $("#c-coupon_code_class_id") .val();
                $.post({
                    url: 'coupon_code_class/getOne',
                    type: 'POST',
                    dataType: 'json',
                    data: {id: a},
                    cache: false,
                    success: function (ret) {
                        if (ret == 2) {
                            $("#brand") . hide();
                        } else if (ret == 1) {
                            $("#brand") . show();
                        } else {
                            $("#brand") . hide();
                        }
                    }, error: function () {
                        Toastr.error(__('Network error'));
                    }
                });

            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});